package com.sjy.system.service.impl;

import java.util.List;

import com.sjy.common.constant.UserConstants;
import com.sjy.common.core.domain.entity.SysDept;
import com.sjy.common.core.text.Convert;
import com.sjy.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjy.system.mapper.SysTreeMapper;
import com.sjy.system.domain.SysTree;
import com.sjy.system.service.ISysTreeService;

/**
 * 树形数据存储Service业务层处理
 * 
 * @author lzm
 * @date 2023-06-19
 */
@Service
public class SysTreeServiceImpl implements ISysTreeService 
{
    @Autowired
    private SysTreeMapper sysTreeMapper;

    /**
     * 查询树形数据存储
     * 
     * @param treeId 树形数据存储主键
     * @return 树形数据存储
     */
    @Override
    public SysTree selectSysTreeByTreeId(Long treeId)
    {
        return sysTreeMapper.selectSysTreeByTreeId(treeId);
    }

    /**
     * 查询树形数据存储列表
     * 
     * @param sysTree 树形数据存储
     * @return 树形数据存储
     */
    @Override
    public List<SysTree> selectSysTreeList(SysTree sysTree)
    {
        return sysTreeMapper.selectSysTreeList(sysTree);
    }

    /**
     * 新增树形数据存储
     * 
     * @param sysTree 树形数据存储
     * @return 结果
     */
    @Override
    public int insertSysTree(SysTree sysTree)
    {
        SysTree info = sysTreeMapper.selectSysTreeByTreeId(sysTree.getParentId());
        if (sysTree.getParentId()!=null){
            sysTree.setAncestors(info.getAncestors() + "," + sysTree.getParentId());
            //把树形类型去掉
            sysTree.setTreeType(null);
        }
        return sysTreeMapper.insertSysTree(sysTree);
    }

    /**
     * 修改树形数据存储
     * 
     * @param sysTree 树形数据存储
     * @return 结果
     */
    @Override
    public int updateSysTree(SysTree sysTree)
    {
        SysTree newParentTree = sysTreeMapper.selectSysTreeByTreeId(sysTree.getParentId());
        SysTree oldTree = sysTreeMapper.selectSysTreeByTreeId(sysTree.getTreeId());
        if (StringUtils.isNotNull(newParentTree) && StringUtils.isNotNull(oldTree))
        {
            String newAncestors = newParentTree.getAncestors() + "," + newParentTree.getTreeId();
            String oldAncestors = oldTree.getAncestors();
            sysTree.setAncestors(newAncestors);
            updateTreeChildren(sysTree.getTreeId(), newAncestors, oldAncestors);
        }
        return sysTreeMapper.updateSysTree(sysTree);
    }

    /**
     * 删除树形数据存储信息
     * 
     * @param treeId 树形数据存储主键
     * @return 结果
     */
    @Override
    public int deleteSysTreeByTreeId(Long treeId)
    {
        return sysTreeMapper.deleteSysTreeByTreeId(treeId);
    }

    @Override
    public boolean checkTreeNameUnique(SysTree sysTree) {
        Long treeId = StringUtils.isNull(sysTree.getTreeId()) ? -1L : sysTree.getTreeId();
        SysTree info = sysTreeMapper.checkDeptNameUnique(sysTree.getTreeName(), sysTree.getParentId());
        if (StringUtils.isNotNull(info) && info.getTreeId().longValue() != treeId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int selectNormalChildrenTreeById(Long treeId) {
        return sysTreeMapper.selectNormalChildrenTreeById(treeId);
    }
    /**
     * 修改子元素关系
     *
     * @param treeId 被修改的节点ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    private void updateTreeChildren(Long treeId, String newAncestors, String oldAncestors)
    {
        List<SysTree> children = sysTreeMapper.selectChildrenTreeById(treeId);
        for (SysTree child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            sysTreeMapper.updateTreeChildren(children);
        }
    }
}
